% First, read in testing data and class information,
% then compute attribute values per epoch.
[T E1 E2 O1] = textread('testing-rec.dat', '%f,%f,%f,%f');
[T2 S]       = textread('testing-hyp.dat', '%f,%f');
L = length(T);
l = 30*100;
D(L/l,20) = 0;
for i=1:1:L/l
  X = (i-1)*l+1:1:min(i*l,L);
  E1_e = E1(X);
  E2_e = E2(X);
  O1_e = O1(X);
 %D(i,1) = mean(abs(E1_e));
 %D(i,2) = mean(abs(E2_e));
 %D(i,3) = std(abs(E1_e));
 %D(i,4) = std(abs(E2_e));
 %D(i,3) = mean((O1_e));
 %D(i,1)  = Spindles(E1_e);
 %D(i,2)  = Spindles(E2_e);
 %D(i,3)  = KComplexes(E1_e);
 %D(i, 4) = KComplexes(E2_e);
  [D(i,1) D(i,2) D(i,3) D(i,4) D(i,5)]  = Frequencies(E1_e);
  [D(i,6) D(i,7) D(i,8) D(i,9) D(i,10)] = Frequencies(E2_e);
end

% Create a Naive Bayes classifier with the data and
% class information.
nb = NaiveBayes.fit(D,S,'Distribution','kernel');
display(nb);

% Read in the validation data set and evaluate its
% attribute values. 
[T E1 E2 O1] = textread('validation-rec.dat', '%f,%f,%f,%f');
[T2 S]       = textread('validation-hyp.dat', '%f,%f');
L = length(T);
for i=1:1:L/l
  X = (i-1)*l+1:1:min(i*l,L);
  E1_e = E1(X);
  E2_e = E2(X);
  O1_e = O1(X);
 %D(i,1) = mean(abs(E1_e));
 %D(i,2) = mean(abs(E2_e));
 %D(i,3) = std(abs(E1_e));
 %D(i,4) = std(abs(E2_e));
 %D(i,3) = mean(abs(O1_e));
 %D(i,7)  = Spindles(E1_e);
 %D(i,8)  = Spindles(E2_e);
 %D(i,9)  = KComplexes(E1_e);
 %D(i,10) = KComplexes(E2_e);
  [D(i,1) D(i,2) D(i,3) D(i,4) D(i,5)]  = Frequencies(E1_e);
  [D(i,6) D(i,7) D(i,8) D(i,9) D(i,10)] = Frequencies(E2_e);
end

% Finally, use the classifier object to predict the class
% labels for the epochs. Then measure the accuracy...
C = predict(nb, D);
correct = 0;
for i=1:1:L/l
   if (C(i) == S(i))
      correct = correct + 1;
   end
end
correct/(L/l)
